package com.oceansoft.mobile.biz.api.controller;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.common.base.entity.Result;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.common.util.StringUtils;

@Controller
@RequestMapping("api/accident")
public class ApiAccidentController {
	
	@RequestMapping("index")
	public String gotoAccidentIndex(HttpServletRequest request, Model model){
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "accident/accident_show";
	}
	
	

    /**
     * 上报事故现场图片(三张)(photos)
     * 同时上传三张现场照片及事故地点坐标(coord)
     *
     * @param request MultipartHttpServletRequest
     * @return Result 生成事故上报业务流水号，在下一步提交时要将此流水号带入
     */
    @RequestMapping(value = "photos/upload", method = POST)
    public Result reportAccidentPhoto(MultipartHttpServletRequest request) {
		List<MultipartFile> files = request.getFiles("test");
		List<File> _files = new ArrayList<File>();
		JSONObject data=new JSONObject();
		data.put("coord", request.getParameter("coord"));
		data.put("position", request.getParameter("position"));
		data.put("photoTaker", request.getParameter("photoTaker"));
		data.put("shootTime", request.getParameter("shootTime"));
		data.put("userId", request.getParameter("userId"));
		for(int i=0;i<files.size();i++){
			CommonsMultipartFile cf= (CommonsMultipartFile)files.get(i); 
	        DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
	        File f = fi.getStoreLocation();
	        _files.add(f);
		}
		return JSONObject.parseObject(HttpClientUtil.submitForm("http://", data, _files), Result.class);
    }

    /**
     * 事故信息处理(双方自行协商1、远程定责2、理赔中心处理三种方式3)，用参数handler区分
     * 三种方式处理完成均需要生成流水号，上传的附件必须进入政务网闸及同时存储在服务器存盘文件
     * 在方式1、3同时需要发送短信通知
     *
     * @param request MultipartHttpServletRequest
     * @return Result 生成事故上报业务流水号，在下一步提交时要将此流水号带入
     */
    @RequestMapping(value = "info/process", method = POST)
    public Result reportAccidentInfo(MultipartHttpServletRequest request) {
    	List<MultipartFile> files = request.getFiles("test");
    	JSONObject data=new JSONObject();
		data.put("accidentId", request.getParameter("accidentId"));
		data.put("handler", request.getParameter("handler"));
		List<File> _files = new ArrayList<File>();
		for(int i=0;i<files.size();i++){
			CommonsMultipartFile cf= (CommonsMultipartFile)files.get(i); 
	        DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
	        File f = fi.getStoreLocation();
	        _files.add(f);
		}
    	return JSONObject.parseObject(HttpClientUtil.submitForm("http://", data, _files), Result.class);
    }

    /**
     * 取消事故信息上报
     *
     * @param userId     用户ID
     * @param accidentId 事故ID
     * @return Result
     */
    @RequestMapping(value = "report/revoke", method = POST)
    public Result revokeAccideintReport(@RequestParam(value = "uid", required = true) String userId,
                                        @RequestParam(value = "accidentId", required = true) String accidentId) {
    	HashMap<String,String> params=new HashMap<String,String>();
    	params.put("uid",userId);
    	params.put("accidentId",accidentId);
    	return JSONObject.parseObject(HttpClientUtil.doPost("http://", params,"UTF-8"),Result.class);
    }
}
